package com.hepinghospital.user.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseDialogFragment;

/**
 * @Description:
 * @Author: lzp
 * @CreateDate: 2023/3/07 14:47
 * @UpdateRemark: 更新说明
 */
public class CustomBottomDialog extends BaseDialogFragment {

    public static CustomBottomDialog newInstance(String select) {
        Bundle args = new Bundle();
        args.putString("select", select);
        CustomBottomDialog fragment = new CustomBottomDialog();
        fragment.setArguments(args);
        return fragment;
    }

    private Dialog mDialog;
    private OnItemClickListener mListener;
    private TextView tvOption1, tvOption2, tvTitle;
    private ImageView ivClose;

    private String title;
    private String option_one;
    private String option_two;
    private String select;

    @SuppressLint("WrongViewCast")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_custom_bottom, container);
        tvTitle = view.findViewById(R.id.tv_title);
        tvOption1 = view.findViewById(R.id.tv_option1);
        tvOption2 = view.findViewById(R.id.tv_option2);
        ivClose = view.findViewById(R.id.iv_close);
        setInfo();
        ivClose.setOnClickListener(v -> {
            dismiss();
        });

        tvOption1.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onConfirm(tvOption1.getText().toString());
                dismiss();
            }
        });

        tvOption2.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onConfirm(tvOption2.getText().toString());
                dismiss();
            }
        });

        if (getArguments() != null) {
            select = getArguments().getString("select");
        }
        if (select.equals(option_one)) {
            tvOption1.setTextColor(ContextCompat.getColor(requireContext(), R.color.mainBlue));
            tvOption2.setTextColor(ContextCompat.getColor(requireContext(), R.color.commonBlack));
        } else if (select.equals(option_two)) {
            tvOption1.setTextColor(ContextCompat.getColor(requireContext(), R.color.commonBlack));
            tvOption2.setTextColor(ContextCompat.getColor(requireContext(), R.color.mainBlue));
        } else {
            tvOption1.setTextColor(ContextCompat.getColor(requireContext(), R.color.commonBlack));
            tvOption2.setTextColor(ContextCompat.getColor(requireContext(), R.color.commonBlack));
        }
        return view;
    }


    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog(savedInstanceState);
        mDialog.setCancelable(false);
        mDialog.setCanceledOnTouchOutside(false);
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity(Gravity.BOTTOM);
        }
        return mDialog;
    }


    public void setText(String title, String option_one, String option_two) {
        this.title = title;
        this.option_one = option_one;
        this.option_two = option_two;
    }


    public void setInfo() {
        tvTitle.setText(title);
        tvOption1.setText(option_one);
        tvOption2.setText(option_two);
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0x00000000));
        getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    }

    public OnItemClickListener getmListener() {
        return mListener;
    }

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }


    public interface OnItemClickListener {

        void onConfirm(String option);

        void onCancel();
    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }


}
